
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import '../manager_state/TapboxA.dart';
import '../manager_state/TapboxB.dart';
import '../manager_state/TapboxC.dart';
import '../router/router2.dart';

void main(){
//  debugPaintSizeEnabled = true;
  runApp(new MyApp());
}
/*
void main() => {
  debugPaintSizeEnabled = true;
  runApp(new MyApp());
};
*/

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    //Widget 任意组件
    Widget titleSection = new Container( // Container 容器
      color: Colors.orange,
      padding: const EdgeInsets.all(32.0),
      child: new Row( // 添加行视图
        children: <Widget>[ // 添加内容
          new Expanded( // 扩展
              child: new Column( // 添加两个列
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Container(
                    padding: new EdgeInsets.only(bottom: 8.0),
                    child: new Text("我是主标题", style: new TextStyle(
                        fontWeight: FontWeight.bold
                    )),
                  ),
                  new Text("我是副标题", style: new TextStyle(
                      color: Colors.grey[500]
                  ),)
                ],
              )
          ),
          // new Icon(Icons.star, color: Colors.red[500]),
          // new Text("41")
          new FavoriteWidget()
        ],
      ),
    );
    // 自定义返回列的方法
    Column buildButtonColumn(IconData icon, String label) {
      Color color = Theme.of(context).primaryColor; // 获取导航的背景色
      return new Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new Icon(icon, color: color,),
          new Container(
            margin: new EdgeInsets.only( top: 8.0),
            child: new Text(label, style: new TextStyle( fontSize: 12.0, color: color, fontWeight: FontWeight.w400),),
          )
        ],
      );
    }
    // 设置按钮
    Widget buttonSection  = new Container(
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          buildButtonColumn(Icons.call, "电话"),
          buildButtonColumn(Icons.router, "导航"),
          buildButtonColumn(Icons.share, "分享"),
        ],
      ),
    );
    
    // 设置文本
    Widget textSection = new Container(
      margin: new EdgeInsets.all(32.0),
      child: new Text("Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese Alps. Situated 1,578 meters above sea level, it is one of the larger Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour walk through pastures and pine forest, leads you to the lake, which warms to 20 degrees Celsius in the summer. Activities enjoyed here include rowing, and riding the summer toboggan run.",
      softWrap: true,
      ),
    );

    return new MaterialApp( // 可以是路由/home/builder中的一个
      title: "导航标题",
      theme: new ThemeData( // 设置主题色
        primaryColor: Colors.red
      ),
      home: new Scaffold( // 一个视图组件
        appBar: new AppBar( // 设置导航
          title: Text('AppBar标题'),
        ),
        body: new ListView( // 设置内容
          children: <Widget>[
            new Image.asset(
              'images/banner_one.png',
              height: 200.0,
              fit: BoxFit.cover,
            ),
            titleSection,
            buttonSection,
            textSection,
            new TapboxA(),
            new ParentWidget(),
            new CParentWidget(),
            new RouterSec()
          ],
        ),
      ),
    );
    /*
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
    */
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: new Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: new Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug paint" (press "p" in the console where you ran
          // "flutter run", or select "Toggle Debug Paint" from the Flutter tool
          // window in IntelliJ) to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

// StatefulWidget 自定义一个可变状态类
class FavoriteWidget extends StatefulWidget {
  /*
  @override
    _FavoriteWidgetState createState() {
      // TODO: implement createState
      return new _FavoriteWidgetState();
    }
*/
  @override
    _FavoriteWidgetState createState() => new _FavoriteWidgetState();
}

class _FavoriteWidgetState extends State<FavoriteWidget> {
  bool _isFavorited = true;
  int _favoriteCount = 41;
  // 按钮点击事件
  void _toggleFavorite(){
    setState(() {
      if (_isFavorited) {
        _favoriteCount -= 1;
        _isFavorited = false;
      } else {
        _favoriteCount += 1;
        _isFavorited = true;
      }
    });
  }

 @override
   Widget build(BuildContext context) {
     // TODO: implement build
     return new Row(
       mainAxisSize: MainAxisSize.min,
       children: <Widget>[
         new Container(
          padding: new EdgeInsets.all(0.0),
          child: new IconButton(
            icon: _isFavorited ? new Icon(Icons.star)  : new Icon(Icons.star_border),
            color: Colors.red[500],
            onPressed: _toggleFavorite,
          ),
         ),
         new SizedBox( // 将文本放在SizedBox中并设置其宽度可防止出现明显的“跳跃” ，因为这些值具有不同的宽度。
          width: 18.0,
          child: new Container(
            child: new Text('$_favoriteCount'),
          ),
         )
       ],
     );
   }
}